/*
 * 
 * 
 */

/*globals jQuery, XinhHem, window */
var XinhHem = (function(XinhHem, $) {
	
	// The create function creates the module object; It does no initialise the object
	XinhHem.rater = function () {
	
		/*
		 * Write private variables and functions here in this closure.
		 * They don't need to be just utility functions, they can refer to the private instance variables 
		 */
		var $element, $rater, $img,
			
			bindRating = function() {
				$rater.find('a').click(function(e) {
					$.ajax($(this).attr('rel'), {
						success: function() {}
					});
					getNextPost();
					e.preventDefault();
				});
			},
			
			getNextPost = function() {
				$.ajax($rater.find('.skip a').attr('rel'), {
					dataType: 'json', 
					success:function(data) {
						console.log(data);
						$img.attr('src', updateUrl($img.attr('src'), data.img_path_medium)); // update img
						$rater.find('a:visible').each(function() {
							$(this).attr('rel', updateUrl($(this).attr('rel'), data.id)); // update rating links
						});
					}
				});
			},
			
			// update url for newly loaded post
			updateUrl = function(url, newVal) {
				return url.slice(0, url.lastIndexOf('/') + 1) + newVal;
			};
			
			
		
		/*
		 * returns a new object that is the functionality of the module
		 * It has access to the private variables and functions declared in this closure.
		 */
		return {

			/*
			 * init Function. Needs to be called to initialise the new module object
			 * 
			 * eg. var myModule = XinhHem.myModuleName()
			 *     myModule.init(); 
			 */
			init: function (elem) { 
				
				/* Cache the jQuery instance of the element(s) this belongs too.
				 * Bake in default selectors. 
				 */
				$element = $(elem || "#post-view");
					
				/* Check this module needs to be initalised for this page */
				if (!$element || !$element.size()) {return;}
			
				/* Do stuff */
				$rater = $element.find("#rater");
				$img = $element.find(".img img");
				bindRating();
				
				/* Return this so it can be chained / assigned
				 * eg. var myModule = XinhHem.myModuleName().init();
				 */
				return this;
			
			}
			
			/* Write Public Methods
			 * These will exist as methods on the new module object
			 * 
			 */
		
		
		};	
	};
	
	/* Return XinhHem after it's been augmented */ 
	return XinhHem;	

}(window.XinhHem || {}, jQuery));
/* End File */
